<template>
  <div>
    <Header></Header>
    <div class="p-40 button-nav">
      <div class="" style="width: 800px;margin: auto">
        <div class="pt-10 pb-10 pl-20 pr-20 bg-grey flex font-14">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>修改密码</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="bg-shadow mt-20 p-40 bg-white">
          <el-form :model="form" label-width="120px"
                   ref="formRef" :rules="rules"
                   style="width: 50%;margin: auto;">
            <el-form-item label="身份证号" prop="id_card">
              <el-input v-model="form.id_card" placeholder="请输入身份证号"/>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" type="password"/>
            </el-form-item>
            <el-form-item label="再次输入密码" prop="password2">
              <el-input v-model="form.password2" placeholder="请再次输入密码" type="password"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">提交修改</el-button>
              <el-button @click="goBack">返回</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import { forgetPass } from "@/api/api";

const rules = {
  id_card: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  password2: [
    { required: true, message: '请再次输入密码', trigger: 'blur' }
  ]
}

export default {
  name: "Login",
  components: {
    Header, Footer
  },
  data() {
    return {
      form: {
        password: '',
        password2: '',
        id_card: ''
      },
      rules: rules
    }
  },
  mounted() {

  },
  methods: {
    onSubmit() {
      const _this = this
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error('请填写或选择所有必填项')
          return false
        }
        if (this.form.password != this.form.password2) {
          this.$message.error('两次密码输入不一致')
          return false
        }
        // operation code
        const data = await forgetPass(this.form)
        console.log('返回',data)
        if (data) {
          this.$message.success('修改成功，请登录')
          localStorage.removeItem('xx-token')
          setTimeout(()=>{
            _this.$router.push('/login')
          }, 1000)
        }
      })
    },
    goBack() {
      this.$router.back()
    },
    register() {
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped>

</style>
